/* 
 * Copyright (C) 2015 Federico Migliavacca, Leonardo Orsello
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package Boundary;

import Control.RegisteredUserFacade;
import Entity.Calendar;
import Entity.RegisteredUser;
import javax.ejb.EJB;
import javax.inject.Named;
import javax.enterprise.context.RequestScoped;
import com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException;
import java.util.Date;
import java.util.GregorianCalendar;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

/**
 *
 * @author fede
 */
@Named(value = "registrationGUI")
@RequestScoped
public class RegistrationGUI {

    @EJB
    private RegisteredUserFacade ruf;

    private RegisteredUser ru;
    private Calendar calendar;

    private Boolean public_bool;

    /**
     * get the manager from control package
     *
     * @return RegisteredUserFacade
     */
    public RegisteredUserFacade getRuf() {
        return ruf;
    }

    /**
     * set the manager from control package
     *
     * @param ruf RegisteredUserFacade
     */
    public void setRuf(RegisteredUserFacade ruf) {
        this.ruf = ruf;
    }

    /**
     * this method get the public or private visibility of event
     *
     * @return boolean value
     */
    public Boolean getPublic_bool() {
        return public_bool;
    }

    /**
     * this method set the public or private visibility to event
     *
     * @param public_bool public=true, private=false
     */
    public void setPublic_bool(Boolean public_bool) {
        this.public_bool = public_bool;
    }

    /**
     * This method get the registerdUser
     *
     * @return RegisteredUser
     */
    public RegisteredUser getRu() {
        if (ru == null) {
            ru = new RegisteredUser();
        }
        return ru;
    }

    /**
     * This method get the registerdUser
     *
     * @param ru RegisteredUser
     */
    public void setRu(RegisteredUser ru) {
        this.ru = ru;
    }

    /**
     * This method provide registration of new user
     *
     * @throws MySQLIntegrityConstraintViolationException check integrity of DB
     */
    public void register() throws MySQLIntegrityConstraintViolationException {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        try {
            calendar.setOwner(ru);
            calendar.setPublic_bool(public_bool);
            ruf.save(ru, calendar);
            facesContext.getApplication().getNavigationHandler().handleNavigation(facesContext, null, "/login");

        } catch (Exception e) {
            ru.setEmail(null);
            facesContext.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error", "User already exists in the current database, please change email address!"));

        }

    }
    
    public Date getCurrentDate(){
        return GregorianCalendar.getInstance().getTime();
    }
    
        public void validateEventDate() {
        if (ru.getBirthdate().after(getCurrentDate())) {

            ru.setBirthdate(getCurrentDate());
        }
        }

    /**
     * Creates a new instance of RegistrationGUI
     */
    public RegistrationGUI() {
        this.public_bool = false;
        this.calendar = new Calendar();
        this.ru = new RegisteredUser();
    }

}
